#include <iostream>
#include <string>
using namespace std;
    
// 输入流的状态检查判断函数
void check_stream_status(){
    cout << "cin.goodbit() = " << cin.good() << endl;
    cout << "cin.badbit() = " << cin.bad() << endl;
    cout << "cin.eofbit() = " << cin.eof() << endl;
    cout << "cin.failbit() = " << cin.fail() << endl;
}

void test(){
    check_stream_status();
    int number;
    cout << "请输入一个数字:" << endl;
    cin >> number;
    cout << number << endl;
    check_stream_status();
    cout << "输入一个错误的输入，如输入字符" << endl;
    cin >> number;
    cout << number;
    check_stream_status();
    cout << "================" << endl;
    //接收一个字符串内容
    string word;
    cin >> word;
    cout << word;
    check_stream_status();
    cout << "++++++++++++++++" << endl;
    if(cin.fail()){
        //清空缓冲区
        cin.clear();
        //忽略内存空间内的多少个字节，到'\n'为止
        cin.ignore(1024,'\n');
    }
    //进行if内的操作之后可以做到调整它的状态不导致输入异常而终止整个程序
    //cin.clear()
    //cin.ignore()
    check_stream_status();
    
}   
    
int main(int atgc, char * argv[])
{   
    test();
    return 0;
}   
    
